﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CoursePack;
using MySql.Data.MySqlClient;
using MySql.Web;

namespace TheSchedulator
{
    public partial class frmLogin : Form
    {

        #region Constructors
        public frmLogin()
        {
            InitializeComponent();
        }
        #endregion

        private void Login_Load(object sender, EventArgs e)
        {

        }

        #region Buttons Events 

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //Close the application
            this.Close();
        }
     
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string User = this.txtUserName.Text;
            string Password = this.txtPassword.Text;


            Student student = MySQLApi.VerifyLogin(User, Password);

            if (student != null)
            {
                this.lblTitle.Text = "Authentication completed !";
                this.lblTitle.BackColor = Color.DarkGreen;
                this.lblTitle.ForeColor = Color.White;
                this.Refresh();

                // Open student home page
                Forms.frmMain frm = new Forms.frmMain(student);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                }
            }
            else
            {
                this.lblTitle.Text = "Your Username or/and Password is incorrect";
                this.lblTitle.BackColor = Color.DarkRed;
                this.lblTitle.ForeColor = Color.White;
                this.timer1.Start();
            }
        }

        private void lblNotRegistered_Click(object sender, EventArgs e)
        {
            // Open student registration form
            Forms.frmRegister frm = new Forms.frmRegister();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }
        }

        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {

            this.lblTitle.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblTitle.ForeColor = Color.White;
            this.lblTitle.Text = "Login Form";
        }

        private void lblNotRegistered_MouseHover(object sender, EventArgs e)
        {
            lblNotRegistered.ForeColor = Color.BlueViolet;
        }

        private void lblNotRegistered_MouseLeave(object sender, EventArgs e)
        {
            lblNotRegistered.ForeColor = System.Drawing.SystemColors.ActiveCaption;
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLogin_Click(null, null);
            }
        }

    }
}
